﻿<CodeSnippetTabbed DocLink="https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxTagBox-2#input-validation">
    <CodeSnippetTabPage Text="Razor">
@(@"<EditForm Model=""@recipients"" OnValidSubmit=""@HandleValidSubmit"" OnInvalidSubmit=""@HandleInvalidSubmit"">
    <DataAnnotationsValidator />
    <p>
        <label for=""emails"">Recipients:</label>
        <DxTagBox Id=""emails""
                  NullText=""Select email recipients""
                  Data=""@Emails.DataSource""
                  TData=""string""
                  TValue=""string""
                  AllowCustomTags=""true""
                  ValidateBy=""TagBoxValidateBy.Tags""
                  @bind-Tags=""@recipients.Data""/>
        </DxTagBox>
        <ValidationMessage For=""@(() => recipients.Data)"" />
    </p>
    <DxButton SubmitFormOnClick=""true"" Text=""Submit"" RenderStyle=""ButtonRenderStyle.Secondary"" />
</EditForm>

@code {
    EmailRecipients recipients = new EmailRecipients();

    private void HandleValidSubmit()
    {
        Console.WriteLine(""OnValidSubmit"");
    }

    private void HandleInvalidSubmit()
    {
        Console.WriteLine(""OnInvalidSubmit"");
    }
}")
    </CodeSnippetTabPage>
    <CodeSnippetTabPage Text="EmailRecipients">
@(@"public class EmailRecipients {
    [Required]
    [EmailAdresses(ErrorMessage = ""Invalid email"")]
    public IEnumerable<string> Data { get; set;}
}")
    </CodeSnippetTabPage>
    <CodeSnippetTabPage Text="EmailAddressesAttribute">
@(@"[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class EmailAdressesAttribute: ValidationAttribute
{
    public override bool IsValid(object value)
    {
        IEnumerable<string> data = value as IEnumerable<string>;
        if (data != null) {
            foreach (var item in data)
                if (!item.Contains(""@"") || !item.Substring(item.IndexOf('@')).Contains("".""))
                    return false;
            return true;
        } else
            return false;
    }
}")
    </CodeSnippetTabPage>
</CodeSnippetTabbed>
